function [hax] = plot_data (dfile, n)
% simple plot of the data with a routine....
% We plot the data in the center frame of the main window, after hiding the list control
% returns handles to the subplots.

global VOLTAGE CURRENT WCHAN
global TMARKS CONTROL

%profile on

hax = [];
if(nargin ~= 2)
    fprintf(2, 'plot_data; wrong number of arguments\n');
    return;
end;

if(isempty(dfile))
    QueMessage('plot_data: call with empty dfile?', 1);
    return;
end;
if((strcmp(dfile.dmode, 'CC') && isempty(VOLTAGE)) || (strcmp(dfile.dmode, 'VC') && isempty(CURRENT)))
    QueMessage('plot_data: called with no data in arrays?');
    return;
end;

% check the figure and make sure we're going to the right one (main window...)
oldfig = gcf; % get current figure - we may have to restore it
hfig = findobj('Tag', 'datac_main');
if(~ishandle(hfig))
    fprintf(2, 'plot_data: Main datac figure window not found?\n');
    return;
end;
figure(hfig); % make it our target.


hax=zeros(3,1);
toggle_display('data');

% frame and figure sizes are in characters - for general compatibility with Unix
figsz=get(hfig, 'Position');
h2=findobj('Tag', 'DataFrame'); % now get the frame we work inside
framesz = get(h2, 'Position');

if(isempty(WCHAN))
    sp1=[0.15 0.27 0.7 0.65];
    sp1f = frameit(sp1, framesz, figsz);
    sp2=[0.15 0.05  0.7 0.18];
    sp2f = frameit(sp2, framesz, figsz);
else
    sp1=[0.15 0.65 0.7 0.27];
    sp1f = frameit(sp1, framesz, figsz);
    sp2=[0.15 0.35  0.7 0.27];
    sp2f = frameit(sp2, framesz, figsz);
    sp3=[0.15 0.05  0.7 0.27];
    sp3f = frameit(sp3, framesz, figsz);
end


fsize=7;
time=make_time(dfile);
tmax = max(max(time));
xunit = 'ms';
yunit = 'Y';

hax(1) = subplot('position', [sp1f.left sp1f.bottom+sp1f.height sp1f.width 0.02]);
set(gca, 'Tag', 'datawin0', 'units', 'normalized');
v = get(gca, 'Xlim');
v(2)=tmax;
set(gca, 'Xlim', [0 v(2)]);
set(gca, 'Xtick', []);
set(gca, 'Ytick', []);

title(sprintf('File: %s  Recs: [%d-%d]', ...
    dfile.filename, dfile.frec, dfile.lrec), 'FontSize',8);
datac_setcrosshair(gca, 'datawin0_info', xunit, yunit, [0.01 0.65 0.08 0.04], [], 0, 'time_marker');

% 4 text displays for index points
tpos = [0.80 0.70 0.08 0.03];
htx = findobj('tag', 'datac_storecursor');
if(isempty(htx))
    htext = uicontrol(...
        'units', 'normalized', ...
        'Position', tpos, ...
        'Style', 'pushbutton', ...
        'string', '->psp_times',  ...
        'callback', 'datac storecursors', ...
        'Tag', 'datac_storecursor');
else % set the position correctly in any case - at the end of the list;
    set(htx, 'Position', tpos);
end;

if(~exist('TMARKS', 'var'))
    TMARKS.t = [5 12 15 60];
    TMARKS.h = [];
else
    if(~isstruct(TMARKS))
        TMARKS.t = [];
        TMARKS.h = [];
    end;
    if(isempty(TMARKS.t))
        TMARKS.t = [5 12 15 60];
    end;
    if(isempty(TMARKS.h))
        TMARKS.h = [];
    end;
end;
sf = getmainselection;
if(~isempty(sf) && length(sf) == 1 && sf > 0)
    if(~check_field(CONTROL(sf),'psp_time'))
        TMARKS.t=[];
    else
        if(ischar(CONTROL(sf).psp_time))
            TMARKS.t = number_arg(CONTROL(sf).psp_time)';
        else
            TMARKS.t = number_arg(CONTROL(sf).psp_time);
        end;
    end;
end;
xu='ms';
hp = findobj('tag', 'datawin0');
j = 0;
for i = 1:length(TMARKS.t)
    j = i-1;
    tpos(2) = tpos(2) - 0.03;
    htx = findobj('tag', sprintf('data_text%d', j));
    if(isempty(htx))
        htext = uicontrol(...
            'units', 'normalized', ...
            'Position', tpos, ...
            'Style', 'pushbutton', ...
            'string', sprintf('%7.2f', TMARKS.t(i)),  ...
            'callback', sprintf('datac_cursorread %d', j), ...
            'Tag', sprintf('data_text%d', j));
    else
        set(htx, 'string' , sprintf('%7.2f', TMARKS.t(i)));
    end;
end;
for i = j+1:32
    htx = findobj('tag', sprintf('data_text%d', i));
    if(~isempty(htx) && ishandle(htx))
        delete(htx);
    end;
end;


hax(2)=subplot('position', [sp1f.left sp1f.bottom sp1f.width sp1f.height]);
hold off
set(hax(2), 'Tag', 'top_plot');
xunit = 'ms';
yunit = 'Y';
tl=(1:size(CURRENT,1));
switch(dfile.dmode)
    case 'CC'
        plot(time(tl,:)', VOLTAGE');
        set(gca, 'Fontsize', fsize);
        set(gca, 'GridLinestyle', ':');
        grid on;
        yunit = 'mV';
        ylabel(sprintf('V (%s)', yunit));
        v = get(gca, 'Xlim');
        v(2)=tmax;
        set(gca, 'Xlim', v);
    case 'VC'
        plot(time(tl,:)', CURRENT');
        set(gca, 'GridLinestyle', ':');
        grid on;
        set(gca, 'Fontsize', fsize);
        yunit = 'pA';
        ylabel(sprintf('I (%s)', yunit));
        v = get(gca, 'Xlim');
        v(2)=tmax;
        set(gca, 'Xlim', v);
    case 'FP'
        [vfp, time] = FP_artsupp(VOLTAGE, dfile, n);
        plot(time(tl,:)', vfp');
        set(gca, 'Fontsize', fsize);
        set(gca, 'GridLinestyle', ':');
        grid on;
        yunit = 'mV';
        ylabel(sprintf('V (%s)', yunit));
        v = get(gca, 'Xlim');
        v(2)=max(max(time))/2;
        set(gca, 'Xlim', v);
    otherwise
end

fprintf(1, 'Plotdata: Setting datawin1\n');

set(gca, 'Tag', 'datawin1', 'units', 'normalized');
xlabel('T (ms)');

datac_setcrosshair(gca, 'datawin1_info', xunit, yunit, [0.01 0.45 0.08 0.04]);
datac_cursorread(-1); % this has to appear AFTER we set up datawin1


hax(3)=subplot('position', [sp2f.left sp2f.bottom sp2f.width sp2f.height]);
hold off
set(hax(3), 'Tag', 'bottom_plot');
xunit = 'ms';
yunit = 'Y';
tl = (1:size(CURRENT,1));
switch(dfile.dmode)
    case 'CC'
        if(~isempty(CURRENT))
            plot(time(tl,:)', CURRENT');
            set(gca, 'Fontsize', fsize);
            set(gca, 'GridLinestyle', ':');
            grid on;
            yunit = 'pA';
            ylabel(sprintf('I (%s)', yunit));
            v = get(gca, 'Xlim');
            v(2)=tmax;
            set(gca, 'Xlim', v);
            u=get(gca, 'YLim');
            for i=1:length(dfile.steps)
                text(dfile.steps(i), u(1), '\uparrow', 'HorizontalAlignment', 'Center');
            end
            set(gca, 'Tag', 'datawin2', 'units', 'normalized');
            xlabel('T (ms)');

            datac_setcrosshair(gca, 'datawin2_info', xunit, yunit, [0.01 0.125 0.08 0.04]);
        end;
    case 'VC'
        if(~isempty(VOLTAGE))
            plot(time(tl,:)', VOLTAGE');
            set(gca, 'Fontsize', fsize);
            set(gca, 'GridLinestyle', ':');
            grid on;
            yunit = 'mV';
            ylabel(sprintf('V (%s)', yunit));
            v = get(gca, 'Xlim');
            v(2)=tmax;
            set(gca, 'Xlim', v);
            %   dv=max(max(voltage)) - min(min(voltage));
            %   steps=find_steps(dfile, voltage, dv/200);
            %   disp(sprintf('Steps: %d, dv = %8.2f', length(steps), dv))
            u=get(gca, 'YLim');
            for i=1:length(dfile.steps)
                text(dfile.steps(i), u(1), '\uparrow', 'HorizontalAlignment', 'Center');
            end
            set(gca, 'Tag', 'datawin2', 'units', 'normalized');
            xlabel('T (ms)');

            datac_setcrosshair(gca, 'datawin2_info', xunit, yunit, [0.01 0.125 0.08 0.04]);
        end;
    otherwise
end

if(~isempty(WCHAN))
    hax(4) = subplot('position', [sp3f.left sp3f.bottom sp3f.width sp3f.height]);
    set(hax(4), 'Tag', 'third_plot');
    xunit = 'ms';
    plot(time(tl,:)', WCHAN');
    set(gca, 'GridLinestyle', ':');
    grid on;
    set(gca, 'Fontsize', fsize);
    yunit = 'arb';
    ylabel(sprintf('W (%s)', yunit));
    axinfo.yunit = 'arb';
    v = get(gca, 'Xlim');
    v(2)=tmax;
    set(gca, 'Xlim', v);
    xlabel('T (ms)');
    set(gca, 'Tag', 'datawin3', 'units', 'normalized');
    datac_setcrosshair(gca, 'datawin3_info', xunit, yunit, [0.01 0.105 0.08 0.04]);
end
if(hfig ~= oldfig)
    figure(oldfig); % restore the one we were called from.
end;


datac_do_ptr(gcbf); % make sure pointer is updated on screen after redrawing.
%profile viewer
return;


% *****************************************************************
%
% *****************************************************************
% compute position of subplot within a frame
function [pos] = frameit (sp, frame, fig)
pos.left = (frame(1)/fig(3))+ sp(1)*(frame(3)/fig(3));
pos.bottom = (frame(2)/fig(4))+sp(2)*(frame(4)/fig(4));
pos.width = sp(3)*(frame(3)/fig(3));
pos.height = sp(4)*(frame(4)/fig(4));
return